package com.itheima.text;

import com.itheima.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/listStudentServlet")
public class ListStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //读取文件信息
        //设置相应乱码问题
//        resp.setContentType("com/itheima/text/html;charset=UTF-8");
        ArrayList<Student> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(getServletContext().getRealPath("/stu.txt")));
        PrintWriter pw = resp.getWriter();
        String str;
        while ((str = br.readLine()) != null) {
            String[] split = str.split(",");
            Student stu = new Student(split[0],split[1],split[2]);
//            pw.write(stu.getUsername()+","+stu.getAge()+","+stu.getScore()+"<br>");
            list.add(stu);
        }
        HttpSession session = req.getSession();
        session.setAttribute("students",list);
        br.close();
        resp.sendRedirect("/day5/listStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
